<?php
class xj extends CI_Controller {
	function index() {
		$this -> pub -> logp('浏览现金流', '1-2');
		$da = $this -> pub -> get_setting('现金流', 'Cash flow');
		$this -> load -> view('adminx/xj', $da);
	}

	function get() {
		$this -> pub -> logp('获取现金流信息', '1-2');
		$day1 = strtotime($this -> input -> post('day1'));
		$day2 = strtotime($this -> input -> post('day2')) + 86400;

		$this -> db -> where('mktime >', $day1);
		$this -> db -> where('mktime <', $day2);

		if ($this -> input -> post('state') != 0) {
			$this -> db -> where('state', $this -> input -> post('state'));
		}
		if ($this -> input -> post('type') != 0) {
			$this -> db -> where('pan_jb.type', $this -> input -> post('type'));
		}
		if ($this -> input -> post('key')) {
			$this -> db -> like('nickname', $this -> input -> post('key'));
		}
		$this -> db -> select('count(*)');
		$this -> db -> join('member', 'pan_member.mid=pan_jb.mid');
		$q = $this -> db -> get_where('jb');
		$ls = $q -> row_array(0);
		$num = $ls['count(*)'];
		//一共的页数
		$da['num'] = ceil($num / 30);

		$page = is_numeric($this -> input -> post('page')) && $this -> input -> post('page') > 0 ? $this -> input -> post('page') : 1;
		$page = $page > $da['num'] ? $da['num'] : $page;
		$da['page'] = $page;

		$page = ($page - 1) == -1 ? 0 : ($page - 1);

		$this -> db -> limit(30, $page * 30);

		$this -> db -> order_by('id', 'desc');
		if ($this -> input -> post('state') != 0) {
			$this -> db -> where('pan_jb.state', $this -> input -> post('state'));
		}
		if ($this -> input -> post('type') != 0) {
			$this -> db -> where('pan_jb.type', $this -> input -> post('type'));
		}
		$this -> db -> select('pan_jb.*,pan_member.headimgurl,pan_member.nickname,pan_member.js,pan_member.yh,pan_member.openid');
		if ($this -> input -> post('key')) {
			$this -> db -> like('nickname', $this -> input -> post('key'));
		}
		$this -> db -> join('member', 'pan_member.mid=pan_jb.mid');

		$this -> db -> where('mktime >', $day1);
		$this -> db -> where('mktime <', $day2);
		$q = $this -> db -> get_where('jb');
		$da['content'] = '';
		$state[1] = '正常';
		$state[2] = '待处理';
		$state[-1] = '作废';

		$type = $this -> config -> item('jb_type');

		foreach ($q->result_array() as $v) {
			$da['content'] .= '<tr>';
			$da['content'] .= '<td>' . $v['id'] . '</td>';
			$da['content'] .= '<td><a target="_blank" href="/iadmin/member/info/' . $v['mid'] . '"><img width="48" src="' . $v['headimgurl'] . '" /></a></td>';
			$da['content'] .= '<td class="tx_name">' . $v['nickname'] . '</td>';
			$da['content'] .= '<td>' . $type[$v['type']] . '</td>';
			$da['content'] .= '<td style="color:#a94442;" class="je">' . $v['je'] . '</td>';
			$da['content'] .= '<td>' . $v['content'] . ($v['type'] == '-1' ? ($v['stime'] ? '提现处理时间：' . date('Y-m-d H:i', $v['stime']) : '提现等待处理') : '') . '</td>';
			$da['content'] .= '<td>' . $v['note'] . '</td>';
			$da['content'] .= '<td>' . date('Y-m-d H:i:s', $v['mktime']) . '</td>';
			$da['content'] .= '<td>' . $state[$v['state']] . '</td>';
			$da['content'] .= '<td><a href="/iadmin/member/info/' . $v['mid'] . '" class="btn"><i class="fa fa-cloud"></i> 查看</a> ';

			if ($v['type'] == 4 || $v['type'] == 5 || $v['type'] == 6) {
				//检索是否已经提现
				$q = $this -> db -> get_where('jb', array('type' => -5, 'did' => '-5-' . $v['id']));
				if ($q -> num_rows() == 0) {
					//$da['content'] .= '<button onclick="send_hb(' . $v['id'] . ',' . $v['mid'] . ')" type="button" class="btn  red"><i class="fa fa-send"></i> 发红包</button>';
				}
			}
			if ($v['sd'] == 1 && $v['type'] == -1) {
				$da['content'] .= '<button onclick="send_sd(' . $v['id'] . ')" type="button" class="btn  red"><i class="fa fa-send"></i> 通过审核</button>';
			}
			$da['content'] .= '</td></tr>';
		}
		$da['type'] = 1;
		echo json_encode($da);
	}

	//审核通过
	function up_sd() {
		$this -> pub -> logp('审核现金流', '1-2');
		$this -> db -> update('jb', array('sd' => 2), array('id' => $this -> input -> post('idx')));

		$da['type'] = 1;

		$q = $this -> db -> get_where('jb', array('id' => $this -> input -> post('idx')));
		$ls = $q -> row_array();

		$this -> db -> select('openid');
		$q = $this -> db -> get_where('member', array('mid' => $ls['mid']));
		$m = $q -> row_array();

		$this -> pub -> send_zd_text($m['openid'], '您的积分提现已审核通过！审核管理员ID：' . $this -> session -> userdata('sid'));

		echo json_encode($da);
	}

	//全部审核通过
	function up_sd_all() {
		$this -> pub -> logp('审核全部现金流', '1-2');
		$this -> db -> update('jb', array('sd' => 2));
		
		$da['type'] = 1;
		echo json_encode($da);
	}

}
?>